//
//  BCWebViewController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/28.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCWebViewController.h"

@interface BCWebViewController ()

/**
 *scriptMessageNames
 */
@property (nonatomic, strong) NSMutableArray *scriptMessageNames;

@end

@implementation BCWebViewController

- (NSMutableArray *)scriptMessageNames {
    if (!_scriptMessageNames) {
        _scriptMessageNames = [NSMutableArray array];
    }
    return _scriptMessageNames;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.customNaviBar.layer.shadowColor = APP_TEXT_COLOR_BLACK.CGColor;
    self.customNaviBar.layer.shadowOffset = CGSizeZero;
    self.customNaviBar.layer.shadowOpacity = 0.2;
    self.customNaviBar.layer.shadowRadius = 2.0;
    
    //
    [self setCloseButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 给webView添加一个关闭按钮
- (void)setCloseButton {
    [self.customNaviBar.rightButton setImage:[LOAD_IMAGE(@"general_close") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    self.customNaviBar.rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 10);
    [self.customNaviBar layoutSubviews];
    self.customNaviBar.rightButton.alpha = 0;
}


#pragma mark - closeButtonDidClickedAction
- (void)didClickedNaviRightButton {
    [super didClickedNaviLeftButton];
}


#pragma mark -
- (void)didClickedNaviLeftButton {
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else {
        [super didClickedNaviLeftButton];
    }
}

#pragma mark - 添加Js方法
- (void)addScriptMessageNames:(NSArray<NSString *> *)names {
    if (names.count) {
        for (NSString *name in names) {
            [self addScriptMessageName:name];
        }
    }
}
- (void)addScriptMessageName:(NSString *)name {
    //因为添加同样的name会崩溃 所以 try catch 一下
    @try {
        [self.webView.configuration.userContentController addScriptMessageHandler:self.scriptMessageHandler name:name];
    } @catch (NSException *exception) {
        NSLog(@"exception %@",exception);
    } @finally {
        //没有exception 添加
        if (![self.scriptMessageNames containsObject:name]) {
            [self.scriptMessageNames addObject:name];
        }
    }
}


#pragma mark - override
// 当main frame导航完成时，会回调
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    [super webView:webView didFinishNavigation:navigation];
    //是否需要加入叉叉按钮
    if ([webView canGoBack]) {
        //需要  判断是否已经加上去了
        self.customNaviBar.rightButton.alpha = 1;
    }else {//不需要就不处理了
        self.customNaviBar.rightButton.alpha = 0;
    }
}


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    [super userContentController:userContentController didReceiveScriptMessage:message];
}




#pragma mark - dealloc
- (void)dealloc {
    if (self.webView) {
        for (NSString *name in self.scriptMessageNames) {
            [self.webView.configuration.userContentController removeScriptMessageHandlerForName:name];
        }
    }
}


@end
